package view;

import java.awt.Color;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.bindings.NotNullObservable;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.layout.HorizontalLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.widgets.tables.Column;
import org.uqbar.arena.widgets.tables.Table;
import org.uqbar.arena.windows.SimpleWindow;
import org.uqbar.arena.windows.WindowOwner;

import aplicationModel.BuscarAsientos;
import asiento.Asiento;

public class BuscarAsientosWindow extends SimpleWindow<BuscarAsientos> {

	public BuscarAsientosWindow(WindowOwner parent) {
		super(parent, new BuscarAsientos());
		this.getModelObject().setParent(this);
	}

	@Override
	protected void createMainTemplate(Panel mainPanel) {
		this.getModelObject().setParent(this);
		this.setTitle(AterrizarWindow.getTituloAplicacion());
		this.setTaskDescription("Ingrese los parámetros de búsqueda");

		super.createMainTemplate(mainPanel);

		this.createResultsGrid(mainPanel);
		this.createGridActions(mainPanel);
	}
	
	@Override
	protected void createFormPanel(Panel mainPanel) {
		Panel searchFormPanel = new Panel(mainPanel, this.getModelObject());
		searchFormPanel.setLayout(new ColumnLayout(4));

		new Label(searchFormPanel).setText("Origen").setForeground(Color.BLUE);
		new TextBox(searchFormPanel).setWidth(150).bindValueToProperty("origen");

		new Label(searchFormPanel).setText("Destino").setForeground(Color.BLUE);
		new TextBox(searchFormPanel).setWidth(150).bindValueToProperty("destino");
		
		new Label(searchFormPanel).setText("Fecha").setForeground(Color.BLUE);
		new TextBox(searchFormPanel).setWidth(100).bindValueToProperty("fecha");
		new Label(searchFormPanel).setText("Ej: 25/09/2012");
	}
	
	@Override
	protected void addActions(Panel actionsPanel) {
		new Button(actionsPanel)
			.setCaption("Buscar")
			.onClick(new MessageSend(this.getModelObject(), "buscar"))
			.setAsDefault()
			.disableOnError();

		new Button(actionsPanel) //
			.setCaption("Limpiar")
			.onClick(new MessageSend(this.getModelObject(), "limpiar"));

	}
	
	protected void createResultsGrid(Panel mainPanel) {
		Table<Asiento> table = new Table<Asiento>(mainPanel, Asiento.class);
		table.setHeigth(200);
		table.setWidth(600);

		table.bindItemsToProperty("resultados");
		table.bindValueToProperty("asientoSeleccionado");

		this.describeResultsGrid(table);
	}

	protected void describeResultsGrid(Table<Asiento> table) {
		new Column<Asiento>(table) //
			.setTitle("Aerolinea")
			.setFixedSize(100)
			.bindContentsToProperty("aerolinea");

		new Column<Asiento>(table) //
		.setTitle("Vuelo")
		.setFixedSize(100)
		.bindContentsToProperty("vuelo");

		new Column<Asiento>(table)//
		.setTitle("N° Asiento")
		.setFixedSize(100)
		.bindContentsToProperty("numeroAsiento");

		new Column<Asiento>(table)//
		.setTitle("Precio")
		.setFixedSize(100)
		.bindContentsToProperty("precio");
		
		new Column<Asiento>(table)//
		.setTitle("Ubicacion")
		.setFixedSize(100)
		.bindContentsToProperty("ubicacionEnAvion");
		
		new Column<Asiento>(table)//
		.setTitle("Clase")
		.setFixedSize(100)
		.bindContentsToProperty("claseAsiento");
	}
	

	protected void createGridActions(Panel mainPanel) {
		Panel actionsPanel = new Panel(mainPanel);
		actionsPanel.setLayout(new HorizontalLayout());

		Button comprar = new Button(actionsPanel)//
		.setCaption("Comprar")
		.onClick(new MessageSend(this.getModelObject(), "comprarAsiento"));

		Button reservar = new Button(actionsPanel)//
		.setCaption("Reservar")
		.onClick(new MessageSend(this.getModelObject(), "reservarAsiento"));
		
		new Button(actionsPanel)//
		.setCaption("Cancelar")
		.onClick(new MessageSend(this, "close"));

		// Deshabilitar los botones si no hay ningún elemento seleccionado en la grilla.
		NotNullObservable elementSelected = new NotNullObservable("asientoSeleccionado");
		comprar.bindEnabled(elementSelected);
		reservar.bindEnabled(elementSelected);
	}
	
	public void sobreReservar(){
		this.getModelObject().sobreReservarAsiento();
	}
	
}
